import pyautogui
import time
import mail
import utility
import my_keys
sendTime = 0
timeLimit = 3600
def placeSoulStone():
    global sendTime
    while True:
    #打开物品栏
        while True:
            thingsBox = pyautogui.locateOnScreen("./images/things.png",confidence=0.9)
            #物品栏已打开
            if thingsBox != None:
                break
            #物品栏未打开
            if thingsBox == None:
                print("未打开物品栏")
                #通过点击物品栏按钮打开物品栏
                box = pyautogui.locateOnScreen("./images/thingButton.png",confidence=0.6)
                if box == None:
                    print("未找到物品栏按钮，请勿遮挡游戏画面,1s后再次尝试打开物品栏")
                    time.sleep(1)
                    continue
                else:
                    cor = pyautogui.center(box)
                    pyautogui.click(cor.x,cor.y)
                #my_keys.key_press(0x0f)
        #此时已打开物品栏
        pyautogui.moveTo(100,5)
        #placedMarbleBox = pyautogui.locateOnScreen("./images/p_marble.png",confidence=0.9)
        placedMarbleBox = utility.findOneFromDir("./images/placedMarbles/",0.9)
        marbleBox = pyautogui.locateOnScreen("./images/marble.png",confidence=0.9)
        dragonBox = pyautogui.locateOnScreen("./images/dragon.png",confidence=0.7)
        dragonBox2 = pyautogui.locateOnScreen("./images/dragon2.png",confidence=0.7)
        dragonBox = dragonBox or dragonBox2
        #放置集魂石
        if placedMarbleBox[0] != None:
            print("已放置集魂石")
            break
        elif marbleBox != None:
            marbleCor = pyautogui.center(marbleBox)
            dragonBox = pyautogui.center(dragonBox)
            pyautogui.moveTo(marbleCor.x,marbleCor.y)
            pyautogui.click()
            time.sleep(1)
            pyautogui.click(dragonBox.x,dragonBox.y)
            time.sleep(2)
            continue
        elif marbleBox == None:
            print("集魂石用完了")
            currentTime = time.time()
            if currentTime - sendTime > timeLimit:
                mail.sendMsg("集魂石用完了")
                sendTime = currentTime
            break
        # if placedMarbleBox == None and marbleBox != None:
        #     marbleCor = pyautogui.center(marbleBox)
        #     dragonBox = pyautogui.center(dragonBox)
        #     pyautogui.moveTo(marbleCor.x,marbleCor.y)
        #     pyautogui.click()
        #     time.sleep(1)
        #     pyautogui.click(dragonBox.x,dragonBox.y)
        # elif placedMarbleBox == None and marbleBox == None:
        #     print("请购买集魂石")
        #     break
        # elif placedMarbleBox != None:
        #     print("已放置集魂石")
        #     break
        

        




